﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Library;

namespace QLKH.PopUp.Khachhang
{
    public partial class popThaycumdongho : Form
    {
        Int64 idcus = -1;
        //private Int16 idduong = -1;
        public popThaycumdongho(Int64 id)
        {
            InitializeComponent();
            idcus = id;
            //idduong = idd;
        }

        private void binCombo(ComboBox cb, DataTable dt, string name, string value, string option)
        {
            List<trangthai> list = new List<trangthai>();
            trangthai ls = null;
            ls = new trangthai();
            ls.name = option;
            ls.value = "0";
            list.Add(ls);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ls = new trangthai();
                    ls.name = dt.Rows[i][name].ToString();
                    ls.value = dt.Rows[i][value].ToString();
                    list.Add(ls);
                }
            }
            cb.DataSource = list;
            cb.DisplayMember = "name";
            cb.ValueMember = "value";
        }

        private void popThaycumdongho_Load(object sender, EventArgs e)
        {
            //dong ho tong
            clDHTong dht = new clDHTong();
            DataTable dtdht = dht.getdata(1);
            binCombo(cbdht, dtdht, "tenDHTong", "id", "<Chọn đồng hồ tổng>");
            //to lap
            //clDH dh = new clDH();
           // DataTable dttl = dh.gettolap();
            //binCombo(cbtolap, dttl, "ten", "id", "<Chọn tổ lắp>");
            //Autocomplete
            //clKhachhang kh = new clKhachhang();
            //AutoCompleteStringCollection dskhachhang = new AutoCompleteStringCollection();
            //DataTable dtds = kh.getautocomplete(idduong, 0);
            //if (dtds != null && dtds.Rows.Count > 0)
            //{
            //    foreach (DataRow row in dtds.Rows)
            //    {
            //        dskhachhang.Add(row["soHD"].ToString());
            //    }
            //}
            //txtkhtong.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            //txtkhtong.AutoCompleteSource = AutoCompleteSource.CustomSource;
            //txtkhtong.AutoCompleteCustomSource = dskhachhang;

            //txtkhvitri.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            //txtkhvitri.AutoCompleteSource = AutoCompleteSource.CustomSource;
            //txtkhvitri.AutoCompleteCustomSource = dskhachhang;
        }

        //private void cbdht_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        Int16 iddhtong = Int16.Parse((cbdht.SelectedValue == null) ? "0" : cbdht.SelectedValue.ToString().Trim());
        //        clKhachhang kh = new clKhachhang();
        //        AutoCompleteStringCollection dskhachhang = new AutoCompleteStringCollection();
        //        DataTable dtds = kh.getautocomplete(idduong, iddhtong);
        //        if (dtds != null && dtds.Rows.Count > 0)
        //        {
        //            foreach (DataRow row in dtds.Rows)
        //            {
        //                dskhachhang.Add(row["soHD"].ToString());
        //            }
        //        }
        //        //txtkhtong.Text = string.Empty;
        //        //txtkhtong.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
        //        //txtkhtong.AutoCompleteSource = AutoCompleteSource.CustomSource;
        //        //txtkhtong.AutoCompleteCustomSource = dskhachhang;

        //        //txtkhtong.Text = string.Empty;
        //        //txtkhvitri.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
        //        //txtkhvitri.AutoCompleteSource = AutoCompleteSource.CustomSource;
        //        //txtkhvitri.AutoCompleteCustomSource = dskhachhang;
        //    }
        //    catch { }
        //}
        //
        public delegate void ThaycumDH(int outid);
        // khai báo 1 kiểu hàm delegate
        public ThaycumDH MyGetData;

        private void btsave_Click(object sender, EventArgs e)
        {
            if (MyGetData != null)
            {
                Int64 id = idcus;
                //Int16 idtolap = Int16.Parse((cbtolap.SelectedValue == null) ? "0" : cbtolap.SelectedValue.ToString().Trim());
                //string tolap = (cbtolap.SelectedValue == null || cbtolap.SelectedValue.ToString() == "0") ? "" : cbtolap.Text.Trim();
                Int16 iddhtong = Int16.Parse((cbdht.SelectedValue == null) ? "0" : cbdht.SelectedValue.ToString().Trim());
                string tendhtong = (cbdht.SelectedValue == null || cbdht.SelectedValue.ToString() == "0") ? "" : cbdht.Text.Trim();
                //string khtong = txtkhtong.Text.Trim();
                //string mavitri = txtkhvitri.Text.Trim();
                //Int16 type = Int16.Parse((rdotruoc.Checked == true) ? "1" : "0");
                Int16 trangthai = Int16.Parse("6");
                clKhachhang clkh = new clKhachhang();
                int outid = clkh.thaycumdh(id, iddhtong, tendhtong, trangthai);
                MyGetData(outid);
            }
            this.Close();
        }

        private void btquit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
